package zsc.ruanc.practicaltraining.mapper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 *
 */
@Component
public class Redis {

    // 相当于redis客户端
    @Autowired
    RedisTemplate redisTemplate;

    /**
     *
     * @param key
     * @param value
     */
    public void set(String key, Object value) {
        // 十分钟有效
        redisTemplate.opsForValue().set(key, value, 600, TimeUnit.SECONDS);
    }

    /**
     *
     * @param key
     * @param value
     * @param time 缓存有效期，单位秒
     */
    public void set(String key, Object value, int time) {
        redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
    }

    public Object get(String key) {
        Object o = redisTemplate.opsForValue().get(key);
        return o;
    }

    public boolean delete(String key) {
        return redisTemplate.delete(key);
    }

    /**
     * 刷新某个key的时间
     * @param key
     * @param time
     */
    public void refresh(String key, int time) {
        Object o = get(key);
        set(key, o, time);
    }


}
